//
//  BaseViewController.m
//  微blog
//
//  Created by Jorn.Wu on 15/8/19.
//  Copyright (c) 2015年 Jorn.Wu(jorn_wza@sina.com). All rights reserved.
//

#import "BaseViewController.h"
#import "ThemeImageView.h"
#import "UIBarButtonItem+Extension.h"
#import "UIProgressView+AFNetworking.h"
#import "ThemeManager.h"

@interface BaseViewController ()

@property (nonatomic, copy) NSString *backgroudImageName;

@end

@implementation BaseViewController

/**
 * 重写dealloc方法，旨在移除观察者
 */
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeDidChangedNotification object:nil];
}


///**
// * 重写initWithCoder方法，旨在让tabbar可以响应切换主题通知
// */
//- (instancetype)initWithCoder:(NSCoder *)aDecoder{
//    if (self = [super initWithCoder:aDecoder]) {
//        
//        self.backgroudImageName = @"bg_home.jpg";
//        
//        /**
//         * 注册成为接收者
//         */
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChangedNotificationAction:) name:kThemeDidChangedNotification object:nil];
//    }
//    
//    return self;
//}
//
//
//
///**
// * 重写initWithNibName方法，旨在方便XIB创建
// */
//- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
//    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
//        
//        self.backgroudImageName = @"bg_home.jpg";
//        
//        /**
//         * 注册成为接收者
//         */
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChangedNotificationAction:) name:kThemeDidChangedNotification object:nil];
//    }
//    return self;
//}


/**
 * 通知的响应方法
 */
- (void)themeDidChangedNotificationAction:(NSNotification *)noti{
    [self reloadBgColorIamge];
}


//--------------------------------------设置背景图片-----------------------------------------------------

/**
 * 重新加载图片
 */
- (void)reloadBgColorIamge{
    ThemeManager *manager = [ThemeManager defualtManager];
    
    /**
     * 通过主题管理对象manager来获取图
     */
    UIImage *image = [manager getThemeImage:self.backgroudImageName];
    
    /**
     * 设置背景图片
     */
    if (image) {
        self.view.backgroundColor = [UIColor colorWithPatternImage:image];
    }
    
}


/**
 * 重新set方法，旨在更改图片
 */
- (void)setBackgroudImageName:(NSString *)backgroudImageName{
    
    if (![_backgroudImageName isEqualToString:backgroudImageName]) {
        _backgroudImageName = [backgroudImageName copy];
        [self reloadBgColorIamge];
    }
    
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
//    统一在这注册通知
    
    self.backgroudImageName = @"bg_home.jpg";
    
    /**
     * 注册成为接收者
     */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChangedNotificationAction:) name:kThemeDidChangedNotification object:nil];
    
}


/**
 * 添加导航栏按钮的方法（普通按钮）
 */
- (void)addNavigationItemWithOrientation:(NSString *)orientation title:(NSString *)title image:(NSString *)imagename action:(SEL)action{
    
    if ([orientation isEqualToString:@"left"]) {
        
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self title:title image:imagename action:action];
    }
    
    if ([orientation isEqualToString:@"right"]) {
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self title:title image:imagename action:action];
    }
}

/**
 * 添加导航栏按钮的方法（主题按钮）
 */
- (void)addNavigationThemeItemWithOrientation:(NSString *)orientation title:(NSString *)title image:(NSString *)imagename action:(SEL)action{
    
    if ([orientation isEqualToString:@"left"]) {
        
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem themeItemWithTarget:self title:title image:imagename action:action];
    }
    
    if ([orientation isEqualToString:@"right"]) {
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem themeItemWithTarget:self title:title image:imagename action:action];
    }
}

/**
 * 状态栏提示
 */
- (void)showStatusTip:(NSString *)title
                 show:(BOOL)show
            operation:(AFHTTPRequestOperation *)operation{
    
    if (!_tipWindow) {      //为空创建
        
        _tipWindow  = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];  //用来装载提示条信息
        _tipWindow.windowLevel = UIWindowLevelStatusBar;
        _tipWindow.backgroundColor = [UIColor blackColor];
        
        //label
        UILabel *tipLabel = [[UILabel alloc] initWithFrame:_tipWindow.bounds];
        tipLabel.backgroundColor = [UIColor clearColor];
        tipLabel.textAlignment = NSTextAlignmentCenter;
        tipLabel.font = [UIFont systemFontOfSize:13.f];
        tipLabel.textColor = [UIColor whiteColor];
        tipLabel.tag = 100;
        [_tipWindow addSubview:tipLabel];
        
        //progress
        UIProgressView *progress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        progress.frame = CGRectMake(0, 20 - 3, kScreenWidth, 5);
        progress.tag = 101;
        progress.progress = 0.0;
        [_tipWindow addSubview:progress];
 
    }
    
    UILabel *tpLabel = (UILabel *)[_tipWindow viewWithTag:100];
    tpLabel.text = title;
    
    
    UIProgressView *progressView = (UIProgressView *)[_tipWindow viewWithTag:101];
    
    if (show) {
        
        _tipWindow.hidden = NO;
        if (operation != nil) {
            
            progressView.hidden = NO;
            //AF 对 UIProgressView的扩展
            [progressView setProgressWithUploadProgressOfOperation:operation animated:YES];
            
        }else {
            progressView.hidden = YES;
        }
  
        
    }else {
        /**
         * 延迟调用
         */
        [self performSelector:@selector(removeTipWindow) withObject:nil afterDelay:1];
    }

}

- (void)removeTipWindow{
    
    _tipWindow.hidden = YES;
    _tipWindow = nil;
}


























- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
